var phonePattern=/1[345678]\d{9}/;
var passwordPattern=/[\S]{6,12}/;
var mc=new Vue({
	el:"#mc",
	data:{
		vid:0,
		infoFlag:true,
		studentFlag:false,
		recruitFlag:false,
		lessonFlag:false,
		coachFlag:false,
		passwordFlag:false,
		infoDisplay:"block",
		studentDisplay:"none",
		recruitDisplay:"none",
		lessonDisplay:"none",
		coachDisplay:"none",
		passwordDisplay:"none",
		captchaDisplay:"block",
		changePasswordDisplay:"none",
		venue:{},
		numsOfCoach:0,
		numsOfStudent:0,
		students:[],
		coaches:[],
		lessons:[],
		phoneNumber:"",
		pswdPhoneNumber:"",
		pswdCaptcha:"",
		captchaBtn:"发送验证码",
		// jinyong:"",
		rePwd:"",
		newPwd:""
	},
	mounted(){
		axios.get("/venue/session").then(res=>{
			console.info("sessionVenue:")
			console.info(res.data.data);
			this.vid=res.data.data.vid;
			console.info("vid:");
			console.info(this.vid)
			axios.get("/venue/find/"+this.vid).then(e=>{
				console.info(e.data.data);
				this.venue=e.data.data;
				this.pswdPhoneNumber=this.venue.phoneNumber;
				// venue=e.data.data;
				console.info(this.venue)
				axios.get("/myStudent/myStudents/"+this.vid)
					.then(e=>{
						console.info(e)
						this.students=e.data.data;
						this.numsOfStudent=this.students.length;
					})
					.catch(err=>{
						console.info(err)
					});
				axios.get("/signing/myCoaches/"+this.vid)
					.then(e=>{
						console.info(e)
						this.coaches=e.data.data;
						this.numsOfCoach=this.coaches.length;
					})
					.catch(err=>{
						console.info(err)
					});
			}).catch(er=>{
				console.info(er)
			});
		}).catch(err=>{
			console.info(err)
		});
		// console.info("传参之前的vid:");//由于异步传输，放在前面这个请求的后面也会先执行，所以后面的vid都是0
		// console.info(this.vid)



	},
	methods:{
		checkInfo:function () {
			if (this.infoFlag===true){
				return;
			}
			if (this.infoFlag===false){
				this.infoFlag=true;
				this.studentFlag=false;
				this.recruitFlag=false;
				this.lessonFlag=false;
				this.coachFlag=false;
				this.passwordFlag=false;
				this.infoDisplay="block";
				this.studentDisplay="none";
				this.recruitDisplay="none";
				this.lessonDisplay="none";
				this.coachDisplay="none";
				this.passwordDisplay="none";
			}
		},
		checkStudents:function () {
			if (this.studentFlag===true){
				return;
			}
			if (this.studentFlag===false){
				this.studentFlag=true;
				this.infoFlag=false;
				this.recruitFlag=false;
				this.lessonFlag=false;
				this.coachFlag=false;
				this.passwordFlag=false;
				this.infoDisplay="none";
				this.studentDisplay="block";
				this.recruitDisplay="none";
				this.lessonDisplay="none";
				this.coachDisplay="none";
				this.passwordDisplay="none";
			}

		},
		checkRecruit:function () {
			if (this.recruitFlag===true){
				return;
			}
			if (this.recruitFlag===false){
				this.studentFlag=false;
				this.infoFlag=false;
				this.recruitFlag=true;
				this.lessonFlag=false;
				this.coachFlag=false;
				this.passwordFlag=false;
				this.infoDisplay="none";
				this.studentDisplay="none";
				this.recruitDisplay="block";
				this.lessonDisplay="none";
				this.coachDisplay="none";
				this.passwordDisplay="none";
			}
		},
		checkLessons:function () {
			if (this.lessonFlag===true){
				return;
			}
			if (this.lessonFlag===false){
				this.infoFlag=false;
				this.studentFlag=false;
				this.recruitFlag=false;
				this.lessonFlag=true;
				this.coachFlag=false;
				this.passwordFlag=false;
				this.infoDisplay="none";
				this.studentDisplay="none";
				this.recruitDisplay="none";
				this.lessonDisplay="block";
				this.coachDisplay="none";
				this.passwordDisplay="none";
			}
			axios.get("/lesson/allLessons/"+this.vid)
				.then(e=>{
					console.info(e)
					this.lessons=e.data.data;
				})
				.catch(err=>{
					console.info(err)
				});
		},
		checkCoaches:function () {
			if (this.coachFlag===true){
				return;
			}
			if (this.coachFlag===false){
				this.lessonFlag=false;
				this.recruitFlag=false;
				this.studentFlag=false;
				this.infoFlag=false;
				this.coachFlag=true;
				this.passwordFlag=false;
				this.infoDisplay="none";
				this.studentDisplay="none";
				this.recruitDisplay="none";
				this.lessonDisplay="none";
				this.coachDisplay="block";
				this.passwordDisplay="none";
			}

		},
		changePassword:function () {
			if (this.passwordFlag===true){
				return;
			}
			if (this.passwordFlag===false){
				this.lessonFlag=false;
				this.recruitFlag=false;
				this.studentFlag=false;
				this.infoFlag=false;
				this.coachFlag=false;
				this.passwordFlag=true;
				this.infoDisplay="none";
				this.studentDisplay="none";
				this.recruitDisplay="none";
				this.lessonDisplay="none";
				this.coachDisplay="none";
				this.passwordDisplay="block";
			}
		},
		broke:function (cid) {
			if (!confirm("你确定要跟这个教练解约吗？")){
				return;
			}
			//发送解约通知
			axios.post("/message/add",{
				"sender":this.venue.vid,
				"target":cid,
				"content":"经过再三考虑，我们场馆决定与你解约！<button @click='broke'>确认解约</button>&nbsp;<button @click='cancel'>拒绝解约</button>",
				"purpose":"解约"
			}).then(e=>{
				console.info(e)
				this.brokeSigning(cid);
				alert(e.data.msg)
			})
				.catch(err=>{
					console.info(err)
				})
		},
		brokeSigning:function(cid){
			axios({
				method:"delete",
				url:"/signing/fire",
				data:{
					"cid":cid,
					"vid":this.vid
				}
			}).then(re=>{
				console.info(re.data.msg)
			}).catch(errorr=>{
				console.info(errorr)
			})
		},
		update:function (e) {
			console.info(e);
		},
		completePhoneNumber:function () {
			console.info("vid："+this.vid)
			console.info("手机号："+this.phoneNumber)
			axios({
				method:"put",
				url:"/venue/phoneNumber",
				data:{
					"vid":this.vid,
					"phoneNumber":this.phoneNumber
				}
			}).then(res=>{
				console.info(res)
				console.info(res.data.msg)
				axios.get("/venue/find/"+this.vid).then(e=>{
					console.info(e.data.data);
					this.venue=e.data.data;
					// venue=e.data.data;
					console.info(this.venue)

				}).catch(er=>{
					console.info(er)
				});
				alert(res.data.msg)
			}).catch(err=>{
				console.info(err)
			})
		},
		send:function () {
			if (this.pswdPhoneNumber===""){
				alert("请先输入手机号");
			}
			if (phonePattern.test(this.pswdPhoneNumber)){
				axios({
					method:"post",
					url:"/venue/captcha/"+this.pswdPhoneNumber
				}).then(res=>{
					console.info(res)
					console.info(res.data.msg)
					if (res.data.code===200){
						this.timer();
						// this.jinyong="disabled";
					}
				}).catch(err=>{
					console.info(err)
				});
			}else{
				alert("请输入正确的手机号")
			}
		},
		timer:function(){
			var t = 15
			var timecount = setInterval(()=>{
				t--
				this.captchaBtn = t
				if(t<0){
					clearInterval(timecount);
					this.captchaBtn = "发送验证码";
					// this.jinyong="";
				}
			},1000)
		},
		testCaptcha:function () {
			console.info("输入的手机号:"+this.pswdPhoneNumber)
			console.info("输入的验证码："+this.pswdCaptcha)
			axios({
				method:"post",
				url:"/venue/check",
				data:{
					"phoneNumber":this.pswdPhoneNumber,
					"captcha":this.pswdCaptcha
				}
			}).then(res=>{
				if (res.data.code===200){
					this.captchaDisplay="none";
					this.changePasswordDisplay="block";
				}
				console.info(res.data.msg)
			}).catch(err=>{

			});
		},
		testNewPswd:function () {
			if (!passwordPattern.test(this.newPwd)){
				alert("密码格式错误，需要6到12位密码")
			}
		},
		testRePswd:function () {
			if (this.newPwd!==""){
				if (!this.newPwd===this.rePwd){
					alert("两次密码输入不一致")
				}
			}
		},
		confirmUpdate:function () {
			if(passwordPattern.test(this.newPwd)&&this.newPwd===this.rePwd){
				axios({
					method:"put",
					url:"/venue/reset",
					data:{
						"vid":this.venue.vid,
						"pwd":this.newPwd
					}
				}).then(res=>{
					console.info(res.data.msg)
					if (res.data.code===200){
						alert("修改密码成功")
						this.captchaDisplay="block";
						this.changePasswordDisplay="none";
						this.changePassword();
					}
				}).catch(err=>{
					console.info(err)
				})
			}

		}
	}
});